using System;
using System.ComponentModel;
using RollManager.Library.ActionSteps.BaseClasses;
using Microsoft.TeamFoundation.Build.Client;
using System.Collections.Generic;
using System.IO;
using RollManager.Library.Dialogs;
using System.Windows.Forms;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class ChooseBuildOutputStep : TeamBuildStep
    {
        private string m_paramName = "BUILD_DIR";
        private int m_maxBuildsToGet = 20;
        private BuildQueryOrder m_queryOrder = BuildQueryOrder.FinishTimeDescending;

        [DisplayName( "Maximum Builds" )]
        [Description( "The maximum number of builds to display for choosing." )]
        public int MaxBuilds
        {
            get
            {
                return m_maxBuildsToGet;
            }
            set
            {
                m_maxBuildsToGet = value;
            }
        }

        [DisplayName( "Parameter Name" )]
        [Description( "The name of the parameter to save for the steps in the script.  This will save the path to the folder of the build chosen so it can be used by subsequent steps." )]
        public string ParamName
        {
            get
            {
                return m_paramName;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Parameter Name cannot be empty." );
                }
                else
                {
                    m_paramName = value.Trim();
                }
            }
        }

        [DisplayName( "Query Order" )]
        [Description( "The order to display the builds." )]
        public BuildQueryOrder QueryOrder
        {
            get
            {
                return m_queryOrder;
            }
            set
            {
                m_queryOrder = value;
            }
        }

        public override string Name
        {
            get { return "Choose Build Output"; }
        }

        protected override void Run()
        {
            // Get service
            IBuildServer buildServer = getBuildServer();

            // Create query
            IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec( TeamProject, BuildName );
            spec.MaxBuildsPerDefinition = m_maxBuildsToGet;
            spec.QueryOrder = m_queryOrder;
            spec.Status = BuildStatus.Succeeded;
            spec.QueryOptions = QueryOptions.None;
            spec.InformationTypes = new string[] { };

            // Get results that still have output folders
            List<IBuildDetail> builds = new List<IBuildDetail>();
            foreach( IBuildDetail build in buildServer.QueryBuilds( spec ).Builds )
            {
                if( Directory.Exists( build.DropLocation ) )
                {
                    builds.Add( build );
                }
            }

            if( builds.Count == 0 )
            {
                throw new Exception( string.Format( "No builds found for {0}", BuildName ) );
            }

            // Create and show dialog to choose build.
            ChooseBuildDialog dialog = new ChooseBuildDialog( builds );
            DialogResult result = dialog.ShowDialog();

            if( result == DialogResult.OK )
            {
                Progress.AddFormat( "Build selected: {0} with location {1}.", 
                    dialog.SelectedBuild.BuildNumber, dialog.SelectedBuild.DropLocation );
                Context.Add( m_paramName, dialog.SelectedBuild.DropLocation );
            }
            else
            {
                throw new Exception( "User cancelled build selection." );
            }
        }

        protected override void Simulate()
        {
            Run();
        }
    }
}
